# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: a1.py
"""

import httpx
import xlwt
style = xlwt.XFStyle()#格式信息
font = xlwt.Font()#字体基本设置
font.name = u'微软雅黑'
style.font = font # 设定样式
wb = xlwt.Workbook()
sheet = wb.add_sheet('sheet1')
head_data = ['分类','店名', '地址', '评分', '标题', '销量', '价格']
for head in head_data:
    sheet.write(0, head_data.index(head), head)

index = 1
def get_http(url,params):
    h_headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
        "Host": "mapi.meituan.com",
        "Origin": "https://g.meituan.com",
        "pragma-uuid": "17cc0a51bf0c8-0253f118e3a38e-b7a1438-1fa400-17cc0a51bf0c8",
        "Referer": "https://g.meituan.com/",
        "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
    }
    res  = httpx.get(url=url,params=params,headers=h_headers)
    if res.status_code == 200:
        return res

def get_cat(page):
    url = 'https://mapi.meituan.com/general/platform/mtlist/navi.bin'
    h_data = {
            "cityid": "70",
            "lat": "28.222291",
            "lng": "112.903588",
            "initcategoryid": "52",
            "categoryid": "52",
            "sort": "0",
            "filter": "",
            "selected": "true",
            "mrnversion": "1",
            "bizid": "1"
        }
    res = get_http(url,params=h_data)
    data = res.json()
    naviBars = data.get('naviBars')
    subs = naviBars[0].get('subs')
    for ii in subs[1:]:
        name  = ii.get('name')
        if name == 'KTV':
            continue
        elif name == "文化艺术":
            continue
        filterId = ii.get('filterId')
        print(name,filterId)
        get_data(page,name,filterId)

def get_data(num,name,fid):
    global index
    i = 0
    end = num * 15
    h_headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
        "Host": "mapi.meituan.com",
        "Origin": "https://g.meituan.com",
        "pragma-uuid": "17cc0a51bf0c8-0253f118e3a38e-b7a1438-1fa400-17cc0a51bf0c8",
        "Referer": "https://g.meituan.com/",
        "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
    }
    while i <= end:
        h_data = {
                "cityid": "70",
                "lng": "112.903588",
                "lat": "28.222291",
                "start": 1 if i == 0 else i,
                "limit": "15",
                "mrnversion": "1",
                "categoryid": fid,
                "bizid": "1",
                "filter": "",
                # "filtername": "",
                "selected": "true",
                'initcategoryid':fid,
                'sort': '0'
            }
        url = 'https://mapi.meituan.com/general/platform/mtlist/filterlist.bin'
        res = httpx.get(url,params=h_data,headers=h_headers)
        print(res,name)
        items = res.json()
        list = items.get('list')
        for ll in list:
            try:
                shopCardInfo = ll.get('shopCardInfo')
                hangInfoVO = shopCardInfo.get('hangInfoVO').get('hangList')
                baseInfoVO = shopCardInfo.get('baseInfoVO')
                for it in hangInfoVO:
                    sale = it.get('sale')  # 销量
                    title = it.get('title') # 标题
                    price = it.get('price') # 价格
                    cbdName = baseInfoVO.get('cbdName')  # 地址
                    shopName = baseInfoVO.get('shopName')  # 店名
                    score = baseInfoVO.get('score')  # 评分
                    items = [name,shopName,cbdName,score,title,sale,price]
                    print(items)
                    # save(items,index)
                    index +=1
            except Exception as e:
                print(e)
                continue
        i = i + 15

def save(data,index):
    print(index)
    maps = lambda x:x if x else x
    # head_data = ['分类','店名', '地址', '评分', '标题', '销量', '价格']
    sheet.write(index, 0, maps(data[0]))
    sheet.write(index, 1, maps(data[1]))
    sheet.write(index, 2, maps(data[2]))
    sheet.write(index, 3, maps(data[3]))
    sheet.write(index, 4, maps(data[4]))
    sheet.write(index, 5, maps(data[5]))
    sheet.write(index, 6, maps(data[6]))
    wb.save("美团.xls")
page = int(input('输入要采集的页数:'))
get_cat(page)

#categoryid: 52
# initcategoryid: 52